﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

/// <summary>
/// 基于世界坐标系的绳子拉取插件，使用时需保证RopeBezier3WorldPostion位于0,0,0点。
/// </summary>
public class RopeBezier3WorldPostion : MonoBehaviour
{
    public XYZ XYZPoint;

    public LineRenderer lineRenderer;
    public int UsePointCount;
    public Transform start;
    public Transform startPoint;
    public Transform endPoint;
    public Transform end;
    public Vector3 UseStartPoint;
    public Vector3 UseEndStartPoint;

    private float angle;
    public float radius = 1;
    public float speed = 10;

    public void Start()
    {
        //UseStartPoint = startPoint.localPosition;
        //UseEndStartPoint = endPoint.localPosition;
    }
    public void OnValidate()
    {
        lineRenderer = GetComponent<LineRenderer>();
    }



    void Update()
    {
        Smooth();
        angle += Time.deltaTime * speed;

        Around1(startPoint, ref UseStartPoint);
        Around2(endPoint, ref UseEndStartPoint);
        CreateLine();
    }


    public void Around1(Transform roatetTras, ref Vector3 basePoint)
    {

        switch (XYZPoint)
        {
            case XYZ.XZ:
                {
                    float x = roatetTras.position.x + radius * Mathf.Cos(angle);
                    float z = roatetTras.position.z + radius * Mathf.Sin(angle);
                    basePoint = new Vector3(x, roatetTras.position.y, z);
                }
                break;
            case XYZ.XY:
                {
                    float x = roatetTras.position.x + radius * Mathf.Cos(angle);
                    float y = roatetTras.position.y + radius * Mathf.Sin(angle);
                    basePoint = new Vector3(x, y, roatetTras.position.z);
                }
                break;
        }
    }
    public void Around2(Transform roatetTras, ref Vector3 basePoint)
    {
        switch (XYZPoint)
        {
            case XYZ.XZ:
                {
                    float x = roatetTras.position.x + radius * Mathf.Sin(angle);
                    float z = roatetTras.position.z + radius * Mathf.Cos(angle);
                    basePoint = new Vector3(x, roatetTras.position.y, z);
                }
                break;
            case XYZ.XY:
                {
                    float x = roatetTras.position.x + radius * Mathf.Sin(angle);
                    float y = roatetTras.position.y + radius * Mathf.Cos(angle);
                    basePoint = new Vector3(x, y, roatetTras.position.z);
                }
                break;
        }

    }
    public void CreateLine(Vector3 midA, Vector3 midB)
    {
        lineRenderer.positionCount = UsePointCount + 1;
        float pointLength = 1 / (float)UsePointCount;
        float add = 0;
        for (int i = 0; i < UsePointCount + 1; i++)
        {


            Vector3 point = Bezier.Bezier3(start.position, midA, midB, end.position, add);
            lineRenderer.SetPosition(i, point);
            add += pointLength;
        }
    }


    [ContextMenu("将线拉到平滑状态")]
    public void Smooth()
    {
        startPoint.localPosition = Vector3.Lerp(start.position, end.position, 0.33f);
        endPoint.localPosition = Vector3.Lerp(start.position, end.position, 0.66f);
        CreateLine();
    }

    [ContextMenu("立即生成线")]
    public void CreateLine()
    {
        CreateLine(UseStartPoint, UseEndStartPoint);

    }
}

